package mobile.models;

import java.util.Date;

import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;

import enums.pay.KQpayPlatform;
import enums.pay.PayPlatform;
import models.FundAccount;
import models.mappers.FundAccountMapper;
import models.mappers.IdentityInfoMapper;
import models.mappers.NoticeMapper;
import play.data.validation.Required;
import vos.Page;

public class IdentityInfo implements java.io.Serializable{
	
	public static final String TABLE_NAME = "user_identity_info";

	public Long id;
	public Long userId;
	@Required
	public PayPlatform bank;
	public String bankName;
	@Required
	public String picUrl;
	@Required
	public String accountNo;
	@Required
	public String accountName;
	@Required
	public String createBank;
	public CheckStatus status;
	public Date createTime;
	public Long checkUserId;
	public Date checkTime;
	public String nick;
	public String address;
	
	public String memo;
	
	public enum CheckStatus{
		
		PASS("审核通过"),
		WAIT_CHECK("待审核"),
		FAIL("审核不通过");
		
		public String title;
		private CheckStatus(String title){
			this.title = title;
		}
	}

	/**
	 * 保存或更新
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @since 2016年12月23日
	 */
	public void save(){
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			FundAccountMapper fundAccountMapper = ss.getMapper(FundAccountMapper.class);
			FundAccount fundAccount = fundAccountMapper.selectBank(this.userId);
			if(fundAccount==null){
				fundAccount = new FundAccount();
				fundAccount.name = this.accountName;
				fundAccount.no = this.accountNo;
				fundAccount.openingBank = this.createBank;
				fundAccount.type = this.bank;
				fundAccount.address = this.address;
				fundAccount.createTime = new Date();
				fundAccount.userId = this.userId;
				fundAccountMapper.insert(fundAccount);
			}else{
				fundAccount.name = this.accountName;
				fundAccount.no = this.accountNo;
				fundAccount.address = this.address;
				fundAccount.openingBank = this.createBank;
				fundAccount.type = this.bank;
				fundAccount.modifyTime = new Date();
				fundAccountMapper.updateById(fundAccount);
			}
			IdentityInfoMapper mapper = ss.getMapper(IdentityInfoMapper.class);
			this.status = CheckStatus.WAIT_CHECK;
			IdentityInfo identityInfo = mapper.selectOneByUserId(userId);
			if(identityInfo==null){
				mapper.insert(this);
			}else{
				mapper.update(this);
			}
		} finally {
			ss.close();
		}
	}
	
	/**
	 * 查询当前身份认证信息
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @since 2016年12月23日
	 * @param userId
	 */
	public static IdentityInfo selectOneByUserId(Long userId){
		IdentityInfo result = null;
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			IdentityInfoMapper mapper = ss.getMapper(IdentityInfoMapper.class);
			result = mapper.selectOneByUserId(userId);
		} finally {
			ss.close();
		}
		return result;
	}
	
	public void value(){
		this.bankName = bank.title;
	}
	
	/**
	 * 审核
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @since 2016年12月23日
	 * @param id
	 * @param status
	 */
	public void check(){
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			IdentityInfoMapper mapper = ss.getMapper(IdentityInfoMapper.class);
			mapper.check(this);
		} finally {
			ss.close();
		}
	}


	/**
	 * 列表查询
	 * @author 刘志远<yuanzhi-2@163.com>
	 * @param status2 
	 * @param nick2 
	 * @since 2016年12月24日
	 * @param vo
	 * @return
	 */
	public static Page<IdentityInfo> findForPage(Page p, String nick, String status) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			IdentityInfoMapper baMapper = ss.getMapper(IdentityInfoMapper.class);
			int count = baMapper.count(nick, status);
			if (count <= 0) {
				return Page.EMPTY;
			}
			Page<IdentityInfo> page = Page.newInstance(p.pageNo, p.pageSize, (int)count);
			page.items = baMapper.listByPage(p,nick,status);
			return page;
		} finally {
			ss.close();
		}
	}
}